package client.backend.order;

import java.io.IOException;
import java.util.StringTokenizer;

import client.backend.Connection;
import server.conn.ConnectionThread;
import server.conn.user.UserConnectionThread;
import server.conn.admin.AdminThread;

/**
 * Order stores data for a specific order between two clients.
 *
 */
public class Order {
	public static final int BUY = 1;
	public static final int SELL = 2;
	
	public static final int ORD_MARKET = 1;
	public static final int ORD_TRAILING_STOP = 2;
	public static final int ORD_LIMIT = 3;
	
	private int id;
	private int userId;
	private String cur1;
	private String cur2;
	private float rate;
	private float amount;
	private long openTime;
	private long closeTime;
	private int bType;
	private int oType;
	private int split;
	
	private float trailPercent;
	
	/**
	 * Public caller for update.
	 * @param input	Input to be used to make order.
	 */
	public Order(String input) {
		update(input);
	}
	
	/**
	 * Private method to set values for this order.
	 * @param input	String of input.
	 */
	private void update(String input) {
		StringTokenizer strTok = new StringTokenizer(input, ConnectionThread.SPLIT);
		id = new Integer(strTok.nextToken());
		userId = new Integer(strTok.nextToken());
		cur1 = strTok.nextToken();
		cur2 = strTok.nextToken();
		rate = new Float(strTok.nextToken());
		amount = new Float(strTok.nextToken());
		openTime = new Long(strTok.nextToken());
		closeTime = new Long(strTok.nextToken());
		bType = new Integer(strTok.nextToken());
		oType = new Integer(strTok.nextToken());
		split = new Integer(strTok.nextToken());
		
		if (oType == ORD_TRAILING_STOP)
			trailPercent = new Float(strTok.nextToken());
		else

			trailPercent = 0F;
	}
	
	/**
	 * Calls update using data read from server.
	 */
	public void update() {
		try {
			String response = Connection.getInstance().readFromServer(UserConnectionThread.ORDER_GET_ONE+ConnectionThread.SPLIT+id);
			update(response);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Attempt to cancel this order and return if successful.
	 * @return	True if order was successful.
	 */
	public boolean cancelOrder() {
		try {
			String response = Connection.getInstance().readFromServer(UserConnectionThread.ORDER_CANCEL+ConnectionThread.SPLIT+id);
			if (!response.startsWith(ConnectionThread.ERROR))
				return true;
			else
				return false;
		} catch (IOException e) {
			return false;
		}
	}
	
	public static Order[] getAllOpen(){
		String[] response = Connection.getInstance().readArray(AdminThread.ORDERS_LIST+"");
		Order[] orders = new Order[response.length];
		for(int i=0;i<response.length;i++){
			orders[i]=new Order(response[i]);
		}
		return orders;
	}
	
	/**
	 * Getter method for order ID.
	 * @return	ID.
	 */
	public int getID() { return id; }
	
	/**
	 * Getter method for user ID.
	 * @return	User ID.
	 */
	public int getUserId() { return userId; }
	
	/**
	 * Getter method for first currency.
	 * @return	First currency.
	 */
	public String getCur1() { return cur1; }
	
	/**
	 * Getter method for second currency.
	 * @return	Second currency.
	 */
	public String getCur2() { return cur2; }
	
	/**
	 * Getter method for ratio.
	 * @return	Ratio.
	 */
	public String getRatio() { return getCur1()+getCur2(); }
	
	/**
	 * Getter method for rate.
	 * @return	Rate.
	 */
	public float getRate() { return rate; }
	
	/**
	 * Getter method for amount.
	 * @return	Amount.
	 */
	public float getAmount() { return amount; }
	
	/**
	 * Getter method for time order was opened.
	 * @return	Open time.
	 */
	public long getOpenTime() { return openTime; }
	
	/**
	 * Getter method for time order was closed.
	 * @return	Close time.
	 */
	public long getCloseTime() { return closeTime; }
	
	/**
	 * Getter method for whether order was a buy or a sell order.
	 * @return	Buy or Sell.
	 */
	public int getBType() {return bType; }
	
	/**
	 * Getter method for the type of order.
	 * @return	Order type.
	 */
	public int getOType() { return oType; }
	
	/**
	 * Getter method for trailing percent.
	 * @return	Trailing percent.
	 */
	public float getTrailPercent() { return trailPercent; }
	
	/**
	 * Getter method for wasSplit.
	 * @return	Whether order has been split or not.
	 */
	public boolean wasSplit() { return (id == split); }
}

